package com.monika.main.system.mq;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author:whh
 * @date: 2024-12-04 20:45
 * <p></p>
 */
@Component
public class MsgPublish {






    public static void publish(String message){

        /**
         * 返回布尔值，表示事件是否发布成功，如果失败可根据此值进行业务逻辑判断
         */
        boolean b = ringBuffer.tryPublishEvent(TRANSLATOR, message);


    }






    private static final EventTranslatorOneArg<MsgEvent,String> TRANSLATOR =  new EventTranslatorOneArg<MsgEvent,String>() {


        @Override
        public void translateTo(MsgEvent event, long sequence, String arg0) {
            event.setData(arg0);
        }
    };

    private static RingBuffer<MsgEvent> ringBuffer;

    @Autowired
    public  void setRingBuffer(RingBuffer<MsgEvent> ringBuffer) {
        MsgPublish.ringBuffer = ringBuffer;
    }
}
